package com.dinstar.dinlink.utils

import android.annotation.SuppressLint
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.dinstar.dinlink.dialog.MessagePermissionsDialog
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jczy.lib_utils.permissions.RxPermissions
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

object PermissionsUtils {

    @SuppressLint("CheckResult")
    fun handleRequestPermissions(
        anyObj: FragmentActivity,
        vararg permissions: String,
        desc: String,
        title: String,
        onGranted: () -> Unit,
        onDenied: (() -> Unit)? = null,
        onDeniedWithoutRequest: (() -> Unit)? = null
    ) {
        val hasPermissions = RxPermissions(anyObj).hasPermissions(*permissions)
        if (hasPermissions) {
            onGranted.invoke()
            return
        }
        //显示说明
        var mpd1: MessagePermissionsDialog? = null
        //    用于多次请求
        mpd1 = MessagePermissionsDialog(message = desc, title = title)

        //申请权限
        RxPermissions(anyObj).requestEachCombined(*permissions).subscribe { permission ->
            mpd1.dismiss()
            if (!permission.denied && permission.granted) {
                //同意授予
                onGranted.invoke()
            } else if (permission.denied && !permission.granted) {
                setHasePermissions(*permissions)
                //拒绝
                onDenied?.invoke()
            } else if (!permission.denied && !permission.granted) {
                setHasePermissions(*permissions)
                //未询问,被拒绝
                onDeniedWithoutRequest?.invoke()
            } else {
                Log.e("=======", "权限else" + permission.toString())
            }
       }
        mpd1.show(anyObj.supportFragmentManager)
    }

    @SuppressLint("CheckResult")
    fun handleRequestPermissions(
        anyObj: Fragment,
        vararg permissions: String,
        desc: String,
        title: String,
        onGranted: () -> Unit,
        onDenied: (() -> Unit)? = null,
        onDeniedWithoutRequest: (() -> Unit)? = null
    ) {
        val hasPermissions = RxPermissions(anyObj).hasPermissions(*permissions)
        if (hasPermissions) {
            onGranted.invoke()
            return
        }
        //显示说明
        var mpd1: MessagePermissionsDialog? = null
        //    用于多次请求
        mpd1 = MessagePermissionsDialog(message = desc, title = title)
        //申请权限
        RxPermissions(anyObj).requestEachCombined(*permissions).subscribe { permission ->
            mpd1.dismiss()
            if (!permission.denied && permission.granted) {
                //同意授予
                onGranted.invoke()
            } else if (permission.denied && !permission.granted) {
                setHasePermissions(*permissions)
                //拒绝
                onDenied?.invoke()
            } else if (!permission.denied && !permission.granted) {
                setHasePermissions(*permissions)
                //未询问,被拒绝
                onDeniedWithoutRequest?.invoke()
            } else {
                Log.e("=======", "权限else" + permission.toString())
            }
        }
        mpd1.show(anyObj.childFragmentManager)
    }

    //被拒绝后的权限存储起来  下次请求再弹出顶部对话框，如果再设置钟同意 不影响其操作
    fun setHasePermissions(vararg permissions: String) {
        var hsm = Cacher.getString("whetherToPopUpTheFrame")
        val dataList: HashSet<String> = if (hsm.isNullOrEmpty()) {
            hashSetOf() // 默认值为空 HashSet
        } else {
            Gson().fromJson(hsm.toString(), object : TypeToken<HashSet<String>>() {}.type)
        }
        permissions.forEach {
            dataList.add(it)
        }
        var json = Gson().toJson(dataList)
        Cacher.set("whetherToPopUpTheFrame", json)
    }


    //获取被拒绝的请求
    fun getHasePerNussuibs(vararg permissions: String): Boolean {
        var isShowDialog = false
        var hsm = Cacher.getString("whetherToPopUpTheFrame")
        val dataList: HashSet<String> = if (hsm.isNullOrEmpty()) {
            hashSetOf() // 默认值为空 HashSet
        } else {
            Gson().fromJson(hsm.toString(), object : TypeToken<HashSet<String>>() {}.type)
        }
        dataList.forEachIndexed { index, s ->
            permissions.forEach {
                if (s.equals(it)) {
                    isShowDialog = s.equals(it)
                    return@forEach
                }
            }
        }
        return isShowDialog
    }
}
